/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * <p>Project: jse2302 - Ex5
 * <p>Powered by webrx On 2023-06-28 08:12:23
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ex5 {

    public static void main(String[] args) {
        //split(); 分割文件10块

        //10块文件合并到一个文件
        merge();
    }

    /**
     * 合并文件
     */
    public static void merge() {
        try(var fos = new FileOutputStream("d:/sys/sys.rar")){
            for (int i = 0; i < 10; i++) {
                try(var fis = new FileInputStream(String.format("d:/sys.rar%02d",i+1))){
                    byte[] buf = new byte[1024*10];
                    int len = 0;
                    while((len = fis.read(buf))>0){
                        fos.write(buf,0,len);
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }

    }


    /**
     * 分割文件
     */
    public static void split() {
        //将文件分割多块  System09_ok.rar01 System09_ok.rar02 System09_ok.rar03
        String path = "d:/sys.rar";
        int num = 10;
        File file = new File(path);
        String fn = file.getName();
        long size = file.length();
        long block = size % num == 0 ? size / num : size / num + 1;
        System.out.println(block);


        try (var fis = new FileInputStream(file)) {
            //byte[] buf = new byte[(int)block];
            byte[] buf = new byte[1024 * 8];
            int len = 0;
            int blocklen = 0;
            boolean f = false;
            int pos = 0;
            ok:
            for (int i = 0; i < num; i++) {
                String npath = String.format("%s/%s%02d", file.getParent(), fn, i+1);
                try (var fos = new FileOutputStream(npath)) {
                    if (f) {
                        fos.write(buf,pos,len-pos);
                        pos = 0;
                        f = false;
                        len = 0;
                        blocklen=0;
                    }
                    while ((len = fis.read(buf)) > 0) {
                        blocklen += len;
                        if (blocklen <= block) {
                            fos.write(buf, 0, len);
                        } else {
                            blocklen -= len;
                            long end = block - blocklen;
                            fos.write(buf,0,(int)end);
                            f = true;
                            pos = (int) end;
                            continue ok;
                        }
                    }
                } catch (Exception e) {
                }
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }


}
